<template>
  <div>
    <a-input @click="showSelectRegion" :placeholder="placeholder" :disabled="disabled" v-model="showValue" select-model="single" readOnly>
      <a-icon slot="suffix" type="solution" />
    </a-input>
    <ant-modal modalWidth="800" modalHeight="500" :visible="open" modal-title="帖子数据" :adjust-size="true" @cancel="cancel">
      <a-row slot="content">
        <!-- 条件搜索 -->
        <div class="table-page-search-wrapper">
          <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
            <a-row :gutter="32">
              <a-col :span="18">
                <a-form-item>
                  <a-input v-model="queryParam.title" placeholder="请输入帖子标题" allow-clear @keyup.enter.native="handleQuery" />
                </a-form-item>
              </a-col>
              <a-col>
                <span class="table-page-search-submitButtons" style="float: right">
                  <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                  <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <a-card :bordered="false">
          <a-table
            :pagination="{
              current: queryParam.pageNum,
              pageSize: queryParam.pageSize,
              total: total,
              showSizeChanger: true,
              showLessItems: true,
              showQuickJumper: true,
              showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
              onChange: changeSize,
              onShowSizeChange: onShowSizeChange
            }"
            tableKey="base-post-index-table"
            @change="handleTableChange"
            rowKey="id"
            size="middle"
            @refresh="getList"
            :columns="columns"
            :data-source="postList"
            :loading="loading"
            :format-conditions="true"
            :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
            :scroll="{ x: 1500 }"
          >
            <span slot="status" slot-scope="text, record">
              <a-badge :status="statusBage(record.status)" :text="statusFormat(record)" />
            </span>
            <span slot="anonymity" slot-scope="text, record">
              {{ anonymityFormat(record) }}
            </span>
            <span slot="type" slot-scope="text, record">
              <a-tag :color="typeColor(record.type)">{{ typeFormat(record) }}</a-tag>
            </span>
            <span slot="image" slot-scope="text">
              <a-avatar shape="square" :src="text" />
            </span>
            <span slot="operation" slot-scope="text, record">
              <a @click="handleChange(record)"> 选择 </a>
            </span>
          </a-table>
        </a-card>
      </a-row>
      <template slot="footer">
        <a-button :disabled="disabled" @click="cancel"> 取消 </a-button>
        <a-button v-if="showValue !== '' ? true : false" type="primary" :disabled="disabled" @click="cancelSelct"> 取消选择 </a-button>
      </template>
    </ant-modal>
  </div>
</template>
<script>
import AntModal from '@/components/pt/dialog/AntModal'
import { listPost, getInitData, getPost } from '@/api/platform/post'
export default {
  name: 'SelectPost',
  components: {
    AntModal
  },
  props: {
    placeholder: {
      type: String,
      default: '请选择'
    },
    // 返回数据
    value: {
      type: String,
      required: false
    },
    disabled: {
      type: Boolean,
      requird: false,
      default: false
    }
  },
  data() {
    return {
      showValue: '',
      open: false,
      // 遮罩层
      loading: true,
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 帖子表表格数据
      postList: [],
      choosePost: undefined,
      // 帖子类型字典
      typeOptions: [],
      // 帖子状态字典
      statusOptions: [],
      anonymityOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        status: '1,4',
        title: undefined
      },
      expandedKeys: [],
      columns: [
        {
          title: '用户',
          dataIndex: 'userName',
          ellipsis: true,
          align: 'left',
          width: '100px'
        },
        {
          title: '地区',
          dataIndex: 'regionName',
          ellipsis: true,
          align: 'left',
          width: '150px'
        },
        {
          title: '版块',
          dataIndex: 'sectionName',
          ellipsis: true,
          align: 'left',
          width: '150px'
        },
        {
          title: '帖子标题',
          dataIndex: 'title',
          ellipsis: true,
          align: 'left',
          width: '180px'
        },
        {
          title: '帖子内容',
          dataIndex: 'content',
          ellipsis: true,
          align: 'left',
          width: '250px'
        },
        {
          title: '帖子封面',
          dataIndex: 'image',
          scopedSlots: { customRender: 'image' },
          ellipsis: true,
          align: 'center',
          width: '100px'
        },
        {
          title: '联系人',
          dataIndex: 'contact',
          ellipsis: true,
          align: 'left',
          width: '80px'
        },
        {
          title: '联系电话',
          dataIndex: 'phone',
          ellipsis: true,
          align: 'left',
          width: '100px'
        },
        {
          title: '类型',
          dataIndex: 'type',
          ellipsis: true,
          align: 'center',
          width: '80px',
          scopedSlots: { customRender: 'type' }
        },
        {
          title: '匿名状态',
          dataIndex: 'anonymity',
          ellipsis: true,
          align: 'center',
          width: '80px',
          scopedSlots: { customRender: 'anonymity' }
        },
        {
          title: '帖子发布时间',
          dataIndex: 'publishTime',
          ellipsis: true,
          align: 'left',
          width: '150px'
        },
        {
          title: '帖子修改时间',
          dataIndex: 'editTime',
          ellipsis: true,
          align: 'left',
          width: '150px'
        },
        {
          title: '点赞数',
          dataIndex: 'likeNumber',
          ellipsis: true,
          align: 'center',
          width: '80px'
        },
        {
          title: '收藏数',
          dataIndex: 'collectNumber',
          ellipsis: true,
          align: 'center',
          width: '80px'
        },
        {
          title: '回复数',
          dataIndex: 'replyNumber',
          ellipsis: true,
          align: 'center',
          width: '80px'
        },
        {
          title: '浏览数',
          dataIndex: 'viewNumber',
          ellipsis: true,
          align: 'center',
          width: '80px'
        },
        {
          title: '举报数',
          dataIndex: 'reportNumber',
          ellipsis: true,
          align: 'center',
          width: '80px'
        },
        {
          title: '帖子状态',
          dataIndex: 'status',
          ellipsis: true,
          align: 'center',
          width: '120px',
          scopedSlots: { customRender: 'status' },
          fixed: 'right'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          align: 'center',
          width: '100px',
          scopedSlots: { customRender: 'operation' },
          fixed: 'right'
        }
      ]
    }
  },
  watch: {
    value: {
      handler(newVal, oldVal) {
        if (!newVal) {
          this.selectedRowKeys = []
          this.showValue = ''
        } else {
          this.setShowValue(newVal)
        }
      }
    }
  },
  created() {
    this.getList()
    getInitData('approve_status,is_anonymity,post_type').then(response => {
      this.statusOptions = response.data.approve_status
      this.anonymityOptions = response.data.is_anonymity
      this.typeOptions = response.data.post_type
    })
    this.setShowValue(this.value)
  },
  methods: {
    getChoosePost() {
      return this.choosePost
    },
    setShowValue(value) {
      if (value !== '' && value !== undefined) {
        getPost(value).then(response => {
          this.showValue = response.data.title
          this.selectedRowKeys.push(response.data.id)
        })
      }
    },
    handleChange(value) {
      this.selectedRowKeys = []
      this.showValue = value.title
      this.selectedRowKeys.push(value.id)
      this.choosePost = value
      this.$emit('change', value.id)
      this.open = false
    },

    showSelectRegion() {
      this.open = true
    },
    cancel(e) {
      this.open = false
    },
    cancelSelct() {
      this.showValue = ''
      this.selectedRowKeys = []
      this.$emit('change', '')
      this.open = false
    },
    /** 查询帖子表列表 */
    getList() {
      this.loading = true
      this.postList = []
      listPost(this.queryParam).then(response => {
        this.postList = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },
    // 帖子状态字典翻译
    statusFormat(row) {
      if (row.status) {
        return this.selectDictLabel(this.statusOptions, row.status)
      } else {
        return ''
      }
    },
    // 帖子匿名状态字典翻译
    anonymityFormat(row) {
      if (row.anonymity) {
        return this.selectDictLabel(this.anonymityOptions, row.anonymity)
      } else {
        return ''
      }
    },
    typeFormat(row) {
      if (row.type) {
        return this.selectDictLabel(this.typeOptions, row.type)
      } else {
        return ''
      }
    },
    // 帖子类型颜色类型
    typeColor(type) {
      if (type === '0') {
        return 'blue'
      } else if (type === '1') {
        return 'green'
      } else if (type === '2') {
        return 'red'
      }
    },
    // 帖子状态徽标数类型
    statusBage(status) {
      if (status === '0') {
        return 'default'
      } else if (status === '1') {
        return 'processing'
      } else if (status === '2') {
        return 'warning'
      } else if (status === '3') {
        return 'error'
      } else if (status === '4') {
        return 'processing'
      } else if (status === '5') {
        return 'error'
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        status: '1,4',
        title: undefined
      }
      this.handleQuery()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      // this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleTableChange(pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    }
  },
  model: { prop: 'value', event: 'change' }
}
</script>
